<?php

namespace App\Http\Controllers\Admin\userController;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
use Crypt;
use Session;

class indexController extends Controller
{
  public function __construct()
  {
    $this->middleware('login');
  }
	public function index(Request $request)
	{
		$num = $request -> input('num',3);
		$keywords = $request -> input('keywords','');
    $data = DB::table('users')->where('uname','like','%'.$keywords.'%')->paginate($num);
    return view('admin.user.addlist', ['data'=>$data,'request'=>$request->all(),'title' => '用户信息']);

	}
	public function changeAddr(Request $request)
	{
		$uid = $request -> uid;

		$addr = $request -> addr;
		$res = DB::table('users')->where('uid',$uid)-> update(['addr'=>$addr]); 
		
	    if ($res) 
	    {
	      	return 1;
	    }else
	    {
	    	return 0;
	    }
	}
    public function edit($uid)
    {
      $data = DB::table('users')-> where('uid','=',$uid)-> first();
      return view('admin.user.edit',['title'=>'用户编辑','data'=> $data]); 
    }
    public function update(Request $request)
    {
       $this -> validate($request,[
              'uname'=>'required|min:3|max:18',
              'tel'=>'digits:11',
              'email'=>'email',
              'uface'=>'required|image|max:2048'
         	],[
               'name.required'=>'用户名不能为空',
               'name.min'=>'用户名最少3位',
               'name.min'=>'用户名最大18位',
               'email.email'=>'请输入正确邮箱格式',
               'tel.digits'=>'请输入11位数字手机号',

         	]);
         $data = $request -> except('_token');
         $uid = $request ->uid;

         if ($request -> hasFile('uface')) 
         {
            if ($request ->file('uface')-> isValid()) 
            {
                 $extension = $request -> file('uface')->extension();
		         $filename = mt_rand(1000,9999).'.'.$extension;
		         $dir = './uploads/avatar/';
		         $request -> file('uface')->move($dir,$filename);
		         $data['uface'] = '/uploads/avatar/'.$filename;
		         //获取老用户头像
                 $oldUface = DB::table('users')->where('uid',$uid)->first()->uface;
		         if (!empty($oldUface)) 
		         {
		         	if ($oldUface != '/uploads/avatar/default.jpeg') 
		         	{
		         		if (file_exists('.'.$oldUface)) 
		         		{
		         			unlink('.'.$oldUface);
		         		}
		         	}
		         }

            }
         }else
         {
         	$data['uface'] = './uploads/avatar/default.jpg';
         }
          
          $res = DB::table('users')->where('uid',$uid)->update($data);
          if ($res)
          {
             return redirect('/admin/user/index')->with(['info'=>'更新成功']);
          }else
          {
          	return back()->with(['info'=>'更新失败']);
          }
         
    }
    public function delete($uid)
    {
     
      $res = DB::table('users')->where('uid',$uid)->delete();

	  if ($res)
          {
             return redirect('/admin/user/index')->with(['info'=>'删除成功']);
          }else
          {
          	return back()->with(['info'=>'删除失败']);
          }
    }
}
